package com.vlin.converter.manager;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.json.JSONObject;

import com.vlin.converter.DeviceInfoConverter;
import com.vlin.entity.mode.DeviceMode;

public class DefaultConverterManager implements ConverterManager {
	
	Map<DeviceMode, DeviceInfoConverter> map = new ConcurrentHashMap<>();
	
	public DefaultConverterManager() {
	}

	@Override
	public boolean support(DeviceMode deviceMode) {
		
		return map.containsKey(deviceMode);
	}

	@Override
	public DeviceInfoConverter getConverter(DeviceMode deviceMode) {
		
		return map.containsKey(deviceMode) ? map.get(deviceMode) : new EmptyDeviceInfoConverter();
	}

	@Override
	public void put(DeviceMode deviceMode, DeviceInfoConverter converter) {
		
		if(converter == null || deviceMode == null)
			return;
		if(converter.supportDeviceType(deviceMode)) {
			this.map.put(deviceMode, converter);
		}
			
	}

	@Override
	public void setConverterMap(Map<DeviceMode, DeviceInfoConverter> map) {
		if(map != null) {
			this.map = map;
		}
	}

	@Override
	public ConverterManager getConverterManager() {
		
		return this;
	}
	
	
	public static class EmptyDeviceInfoConverter implements DeviceInfoConverter{

		@Override
		public JSONObject convertToFront(JSONObject params, DeviceMode deviceMode) {
			return new JSONObject();
		}

		@Override
		public JSONObject convertToBack(JSONObject params, DeviceMode deviceMode) {
			return new JSONObject();
		}

		@Override
		public boolean supportDeviceType(DeviceMode deviceMode) {
			return false;
		}
		
	}
	

}
